package controllers;




import java.util.ArrayList;
import java.util.List;

import models.AutocompleteValue;
import models.Usuario;

import play.mvc.Controller;

/**
 * Controller that provides Ajax actions for a jQuery UI Autocomplete component.
 */
public class Autocomplete extends Controller {

	/**
	 * Used to limit the size of auto-complete lists to a reasonable number of results.
	 */
	public static int AUTOCOMPLETE_MAX = 10;
	
	
	/**
	 * Returns a JSON representation of a list of Strings, to populate an autocomplete input field.
	 * The list is filtered by the given search term, and limited to a maximum length - so that there
	 * are not more results than you will reasonably select from.
	 *
	 * @param term The text entered in the input field, used to filter the results.
	 */
	public static void label(final String term) {
		List<Usuario> usuariosBuscados = Usuario.find("lower(usu_nome) like ?", "%"+term+"%")
				.fetch();
		final List<String> response = new ArrayList<String>();

		for (Usuario label : usuariosBuscados) {
			if (label.usu_nome.startsWith(term.toLowerCase())) {
				response.add(label.usu_nome_completo);
			}
			if (response.size() == AUTOCOMPLETE_MAX) {
				break;
			}
		}
		renderJSON(response);
	}

	/**
	 * Returns a JSON representation of a list of items, each of which has a label and a value.
	 * The value uses the {@link AutocompleteValue} type, which has the correct JSON representation
	 * for the jQuery UI autocomplete plug-in.
	 *
	 * @param term The text entered in the input field.
	 */
	

}
